package com.dylovedz.javafxphone.service;

import cn.hutool.core.bean.BeanUtil;
import com.dylovedz.javafxphone.controller.vo.WechatExcelVO;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveTask;

public class MaterialTask extends RecursiveTask<List<WechatExcelVO>> {

    // 每个小任务最多执行10次
    private static final int MAX = 10;

    private int start;
    private int end;
    private WxMpServiceImpl wxMpService;
    private String outDir;
    private WechatService wechatService;

    public MaterialTask(WxMpServiceImpl wxMpService, WechatService wechatService, int start, int end, String outDir) {
        this.wxMpService = wxMpService;
        this.wechatService = wechatService;
        this.start = start;
        this.end = end;
        this.outDir = outDir;
    }

    @Override
    protected List<WechatExcelVO> compute() {

        if (end - start < MAX) {
            List<WechatExcelVO> wechatExcelList = new ArrayList<>();
             /*WxMpFreePublishList publicationRecords = wxMpService.getFreePublishService().getPublicationRecords(offset, 20);
                int publishCount = publicationRecords.getTotalCount();
                for (WxMpFreePublishItem item : publicationRecords.getItems()) {
                    for (WxMpFreePublishArticles article : item.getContent().getNewsItem()) {
                        WechatExcelVO articleItem = new WechatExcelVO();
                        BeanUtil.copyProperties(article, articleItem);
                        wechatExcelList.add(articleItem);
                        wechatService.saveHtml2Word(outDir, articleItem, totalPublish);
                        totalPublish += 1;
                    }
                }*/
            for (int i = start; i<= end; i++) {
                try {
                    int offset = i * 20;
                    WxMpMaterialNewsBatchGetResult wxMpMaterialNewsBatchGetResult = wxMpService.getMaterialService().materialNewsBatchGet(offset, 20);
                    int newsCount = wxMpMaterialNewsBatchGetResult.getTotalCount();
                    int itemIndex = 0;
                    for (WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem item : wxMpMaterialNewsBatchGetResult.getItems()) {
                        int orderIndex = 0;
                        for (WxMpNewsArticle article : item.getContent().getArticles()) {
                            WechatExcelVO articleItem = new WechatExcelVO();
                            BeanUtil.copyProperties(article, articleItem);
                            articleItem.setOffset(offset + itemIndex);
                            articleItem.setOrder(orderIndex);
                            wechatExcelList.add(articleItem);
                            try {
                                wechatService.saveHtml2Word(outDir, articleItem);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            orderIndex++;
                        }
                        itemIndex++;
                    }
                    System.out.println(newsCount);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
            return wechatExcelList;
        } else {
            System.out.println("任务分解");
            int middle = start + ((end - start) / 2);
            MaterialTask left = new MaterialTask(wxMpService, wechatService, start, middle, outDir);
            MaterialTask right = new MaterialTask(wxMpService, wechatService, middle + 1, end, outDir);
            // 并行执行两个小任务
            left.fork();
            right.fork();
            List<WechatExcelVO> leftList =  left.join();
            List<WechatExcelVO> rightList = right.join();
            leftList.addAll(rightList);
            return leftList;
        }

    }
}
